<?php
declare (strict_types = 1);
namespace app\user\logic\teacher;

use app\common\exception\LogicException;
use app\user\model\teacher\MaterialCategory as MaterialCategoryModel;

class MaterialCategory extends MaterialCategoryModel
{

    /**
     * 讲师数据验证
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-08
     * @return [type] [description]
     */
    public function checkAndReturnTeacher()
    {
        // 讲师权限
        $mid     = static::$mid;
        $teacher = app(Teacher::class)->where('user_id', $mid)->findOrEmpty();
        if ( $teacher->isEmpty() ) throw new LogicException("讲师权限错误");
        return $teacher;
    }

    /**
     * 添加素材分组
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function createCategory(array $param): void
    {
        $teacher = $this->checkAndReturnTeacher();
        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($param);
        // 参数
        $type   = intval($param['type']);
        $title  = $param['title'];

        $re = $this::create([
            'teacher_id'    => $teacher->id,
            'material_type' => $type,
            'title'         => $title,
        ]);
        
        if ( $re->isEmpty() ) throw new LogicException("创建失败");
        
    }

    /**
     * 修改素材分组
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function updateCategory(array $param): void
    {
        $teacher = $this->checkAndReturnTeacher();
        // 验证数据
        $this->getValidate()->scene('update')->failException()->check($param);
        // 参数
        $id     = intval($param['id']);
        $title  = $param['title'];

        $data = $this->findOrFail($id);
        
        $data->title = $title;

        if ( !$data->save() ) throw new LogicException("修改失败");
        
    }

    /**
     * 删除素材分组
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function deleteCategory(array $param): void
    {
        $teacher = $this->checkAndReturnTeacher();
        // 验证数据
        $this->getValidate()->scene('delete')->failException()->check($param);
        // 参数
        $id     = intval($param['id']);

        $data = $this->findOrFail($id);

        if ( !$data->delete() ) throw new LogicException("删除失败");
        
    }

    /**
     * 获取素材分组，包含默认和全部选项，每组所包含的素材数量
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-07
     */
    public function getCategoryList(array $param)
    {
        // 讲师权限
        $teacher = $this->checkAndReturnTeacher();
        // 验证数据
        $this->getValidate()->scene('list')->failException()->check($param);
        // 参数
        $type = intval($param['type']??1);
        $in   = intval($param['in']??1);

        $list = $this->field('id,title')
            ->withCount(['material'])
            ->type($type)
            ->teacher($teacher->id)
            ->select()->toArray();
            
        $def_count = app(Material::class)->type($type)->teacher($teacher->id)->default()->count();

        if ( 1 != $in ) array_unshift($list, ['id' => -1, 'title' => '默认分组', 'material_count' => $def_count]);
        
        if ( 0 == $in ) array_unshift($list, ['id' => 0,  'title' => '全部分组', 'material_count' => array_sum(array_column($list, 'material_count'))]);

        return $list;
    }


}
